package com.example.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commons.core.entity.RoleMenuRelation;
import com.example.commons.core.entity.SystemMenu;
import com.example.commons.core.entity.UserInfo;
import com.example.commons.core.enums.RoleEnum;
import com.example.system.mapper.SystemMenuMapper;
import com.example.system.service.RoleMenuRelationService;
import com.example.system.service.SystemMenuService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> implements SystemMenuService {

    private final RoleMenuRelationService roleMenuRelationService;

    public SystemMenuServiceImpl(RoleMenuRelationService roleMenuRelationService) {
        this.roleMenuRelationService = roleMenuRelationService;
    }


    @Override
    public List<Tree<String>> getRouteMenuList(Long roleId) {
        List<SystemMenu> routeMenuList = baseMapper.getRouteMenuList(roleId);

        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        return TreeUtil.build(routeMenuList, "0", (systemMenu, tree) -> {
            tree.setId(systemMenu.getMenuId().toString());
            tree.setParentId(systemMenu.getParentId().toString());
            tree.setName(systemMenu.getName());
            tree.setWeight(systemMenu.getSort());
            // 扩展属性 ...
            Map<String, Object> meta = new HashMap<>(8);
            meta.put("title", systemMenu.getTitle());
            meta.put("isLink", systemMenu.getIsLink());
            meta.put("isHide", systemMenu.getIsHide());
            meta.put("isKeepAlive", systemMenu.getIsKeepAlive());
            meta.put("isAffix", systemMenu.getIsAffix());
            meta.put("isIframe", systemMenu.getIsIframe());
            meta.put("icon", systemMenu.getIcon());

            tree.putExtra("path", systemMenu.getPath());
            tree.putExtra("component", systemMenu.getComponent());
            tree.putExtra("meta", meta);
            tree.putExtra("redirect", systemMenu.getRedirect());
            // 前端el-tree需要使用该属性
//            tree.putExtra("title", systemMenu.getTitle());
        });
    }

    @Override
    public void addSystemMenu(SystemMenu systemMenu) {
        // 插入Menu
        this.saveOrUpdate(systemMenu);
        // 获取当前用户信息
        UserInfo userInfo = (UserInfo) StpUtil.getSession().get("userInfo");
        // 仅给管理员添加当前新增的Menu
        if (Objects.equals(userInfo.getRoleId(), RoleEnum.ADMIN.getCode())) {
            // 删除角色与Menu的关联
            roleMenuRelationService.remove(new LambdaQueryWrapper<RoleMenuRelation>().eq(RoleMenuRelation::getRoleId, userInfo.getRoleId()).eq(RoleMenuRelation::getMenuId, systemMenu));
            // 新增角色与Menu的关联
            roleMenuRelationService.save(RoleMenuRelation.builder().menuId(systemMenu.getMenuId()).roleId(userInfo.getRoleId()).build());
        }

    }
}
